﻿using System;
using System.Linq;

namespace SuggestionService
{
    public class DistanceCalculator
    {
        public static double CalculatePearson(int [] x, int [] y)
        {
            var xAvg = x.Select((t, i) => i).Sum()/x.Length;
            var yAvg = y.Select((t, i) => i).Sum()/y.Length;

            double num = 0.0;
            for(int i = 0; i < x.Length; i++)
            {
                num += (x[i] - xAvg)*(y[i] - yAvg);
            }

            double den = 0.0;
            var sum1 = 0;
            var sum2 = 0;
            for(int i = 0; i < x.Length; i++)
            {
                sum1 += (x[i] - xAvg)*(x[i] - xAvg);
                sum2 += (y[i] - yAvg)*(y[i] - yAvg);
            }
            den = Math.Sqrt(sum1*sum2);

            return den == 0 ? 0 : num/den;
        }

        public static double CalculateEuclidian(int [] x, int [] y )
        {
            double sum = 0;
            for(int i = 0; i < x.Length; i++)
            {
                sum += (x[i] - y[i])*(x[i] - y[i]);
            }
            sum = Math.Sqrt(sum);
            return Normalize(sum);
        }

        private static double Normalize(double value)
        {
            return 1/(1 + value);
        }
    }

}